
import 'package:animated_interpolation/smart_tabbar.dart';
import 'package:animated_interpolation/sweet_indicator_painter.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/pages/xspace/post/xpost_page.dart';
import 'package:find_friend/pages/xspace/school/xallschool_page.dart';
import 'package:find_friend/pages/xspace/school/xfollow_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';
import 'package:underline_indicator/underline_indicator.dart';

class XPage extends StatefulWidget {
  _XPageState _state;

  void refresh() {
    _state._refreshData();
  }
  @override
  _XPageState createState() => _state = _XPageState();
}

class _XPageState extends State<XPage> with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin{

  TabController _tabController;
  PageController _pageController;
  XAllSchoolPage allscholl = XAllSchoolPage(schooltype: 0,); // 所有
  XAllSchoolPage customscholl = XAllSchoolPage(schooltype: 1);//本校
  XAllSchoolPage otherscholl = XAllSchoolPage(schooltype: 2);// 非本校
  bool ishot = true;
  int currTabindex = 0;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();

    _tabController = TabController(length: 3, vsync: this);
    _pageController = PageController(initialPage: 0);
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    _tabController.dispose();
    _pageController.dispose();
    super.dispose();
  }

  _refreshData({dynamic data}) {
    // 更新自己发布的
    if(_tabController.index == 0) {
      allscholl.refreshData(data: data, hot: ishot);
    } else if(_tabController.index == 1) {
      customscholl.refreshData(data: data, hot: ishot);
    } else {
      otherscholl.refreshData(data: data, hot: ishot);
    }
  }

  TextStyle _getTextStyle(int index) {
    return index == currTabindex ? instanceTextStyle(17, APP_font_black) : instanceTextStyle(14, const Color(0xff666666));
  }

  @override
  Widget build(BuildContext context) {
    final padding = MediaQuery.of(context).padding;
    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: APP_scaffold_back_color,
      drawer: XFollowPage(),
      appBar: AppBar(
        centerTitle: true,
        title: Text.rich(TextSpan(
          children: [
            TextSpan(
              text: "X",
              style: TextStyle(
                fontSize: 15,
                fontFamily: "BalooBhaina",
              )
            ),
            TextSpan(
              text: "区",
              style: TextStyle(
                fontSize: 17,
                fontFamily: "BalooBhaina",
              )
            ),
          ]
        )),
        backgroundColor: Colors.white,
        elevation: 0,
        leading: Container(),
        actions: [
          InkWell(
            onTap: () {
              Navigator.pushNamed(context, '/search');
            },
            child: Image.asset("assets/sousuo.png", width: 30,)
          ),
          GestureDetector(
            onTap: () async {
              dynamic data = await push(context, XPostPage());
              if(data != null) _refreshData(data: data);
            },
            child: Image.asset("assets/fabu2.png", width: 30,),
          )
        ],
      ),
      floatingActionButton: Container(
        padding: EdgeInsets.only(bottom: 50 + padding.bottom / 2),
        child: IconButton(
          icon: Image.asset("assets/huidaodingbu.png"),
          onPressed: (){
            switch (_tabController.index) {
              case 0:
                allscholl.goTop();
                break;
              case 1:
                customscholl.goTop();
                break;
              default:
                otherscholl.goTop();
                break;
            }
          },
        ),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Container(
            color: Colors.white,
            height: 40,
            padding: const EdgeInsets.symmetric(horizontal: 9.5),
            child: Row(
              children: [
                InkWell(
                  onTap: (){
                    _scaffoldKey.currentState.openDrawer();
                    // Navigator.push(context, PageTransition(
                    //   child: XFollowPage(),
                    //   type: PageTransitionType.leftToRight,
                    //   alignment: Alignment.bottomLeft
                    // ));
                  },
                  child: Row(
                    children: [
                      Image.asset("assets/guanzhu.png"),
                      Text("关注", style: instanceTextStyle(11, APP_font_grey),)
                    ],
                  ),
                ),
                Expanded(
                  child: SizedBox(
                    height: 40,
                    child: SmartTabBar(
                      controller: _tabController,
                      labelColor: APP_font_black,
                      indicatorColor: APP_main_color,
                      indicatorBuilder: (context,controller,indicator,indicatorSize,tabKeys,old)=>SweetIndicatorPainter(
                        controller: controller,
                        indicatorSize: indicatorSize,
                        indicator: indicator,
                        tabKeys: tabKeys,
                        old: old
                      ),
                      onTap: (index){
                        if(index == currTabindex) {
                          _refreshData();
                          return;
                        }
                        setState(() {
                          currTabindex = index;
                        });
                        _pageController.jumpToPage(index);
                      },
                      tabs: [
                        Container(child: Text("所有", style: _getTextStyle(0),)),
                        Text("本校", style: _getTextStyle(1),),
                        Text("非本校", style: _getTextStyle(2),),
                      ],
                    ),
                  ),
                ),
                InkWell(
                  onTap: (){
                    setState(() {
                      ishot = !ishot;
                    });
                    _refreshData();
                  },
                  child: Container(
                    height: 40,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                      Text(ishot ? "热":"新", style: instanceTextStyle(11, ishot ? APP_verify_error_color : const Color(0xff00CEE6))),
                      SizedBox(width: 2.5,),
                      Image.asset("assets/re.png", color: ishot ? APP_verify_error_color : const Color(0xff00CEE6),)
                    ]),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: PageView(
              physics: const ClampingScrollPhysics(), // 关闭回弹效果
              controller: _pageController,
              onPageChanged: (index){
                setState(() {
                  currTabindex = index;
                });
                _tabController.animateTo(index, duration: const Duration(milliseconds: 300), curve: Curves.easeIn);
              },
              children: [
                allscholl,
                customscholl,
                otherscholl,
              ],
            ),
          )
        ],
      ),
    );
  }
}